//
//  FiveView.m
//  相册罚写5
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FiveView.h"
#import "FiveScrollView.h"

@interface FiveView ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, assign) NSInteger prePage;

@end

@implementation FiveView


- (void)dealloc
{
    [_scrollV release];
    [_pageC release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _prePage = 1;
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.backgroundColor = [UIColor whiteColor];

    self.scrollV = [[UIScrollView alloc] initWithFrame:self.frame];
    self.scrollV.contentSize = CGSizeMake(self.frame.size.width * 21, 0);
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollV.delegate = self;
    self.scrollV.pagingEnabled = YES;
    [self addSubview:self.scrollV];
    [_scrollV release];
    

    for (NSInteger i = 0; i < 21; i++) {
        
        FiveScrollView *fiveSV = [[FiveScrollView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        NSString *fileName = nil;
        if (0 == i) {
            fileName = @"image19.jpg";
        } else if (20 == i) {
            fileName = @"image1.jpg";
        } else {
            fileName = [NSString stringWithFormat:@"image%ld.jpg", i];
        }
        fiveSV.imageV.image = [UIImage imageNamed:fileName];
        [self.scrollV addSubview:fiveSV];
        [fiveSV release];
        
        
    }
    
    
    
    
    
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(30, 100, 300, 30)];
    self.pageC.numberOfPages = 19;
    [self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.pageC];
    [_pageC release];
    



}

- (void)pageAction:(UIPageControl *)sender
{
    self.scrollV.contentOffset = CGPointMake(self.frame.size.width * (sender.currentPage + 1), 0);
 
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.scrollV) {
        NSInteger temp = scrollView.contentOffset.x / self.frame.size.width;
        
        if (0 == temp) {
            self.scrollV.contentOffset = CGPointMake(self.frame.size.width * 19, 0);
            temp = 19;
        }
        if (20 == temp) {
            self.scrollV.contentOffset = CGPointMake(self.frame.size.width, 0);
            temp = 1;
        }
        if (self.prePage != temp) {
            [[self.scrollV.subviews objectAtIndex:self.prePage] setZoomScale:1.0];
            self.pageC.currentPage = temp - 1;
            self.prePage = temp;
            
        }
        
        
    }

    
    
    
    
    
}















@end
